package com.epam.task.web.db;

import com.epam.task.web.util.logging.LoggerUtil;
import org.apache.log4j.Logger;

import java.sql.*;

public class DataBaseReader extends DataAccessLayer {

    LoggerUtil logger = new LoggerUtil(Logger.getLogger(DataBaseReader.class));

    public ResultSet select(String tableName, Object... fieldsConditions) throws SQLException {
        Connection connection = super.getPool().createConnection();
        PreparedStatement select = null;
        String selectQuery = null;
        ResultSet resultSet = null;
        int fieldsConditionsNamesEnd = fieldsConditions.length / 2;

        try {
            connection.setAutoCommit(false);

            selectQuery = String.format("SELECT * FROM %s WHERE ", tableName);

            for (int i = 0; i < fieldsConditionsNamesEnd; i++) {
                if ((fieldsConditions.length > 2) && (i != 0)) {
                    selectQuery = selectQuery.concat(" and");
                }
                selectQuery = selectQuery.concat(String.format(" %s = ?", fieldsConditions[i]));
            }
            selectQuery = selectQuery.concat(";");

            select = connection.prepareStatement(selectQuery);
            for (int i = fieldsConditionsNamesEnd, j = 0; i < fieldsConditions.length; i++, j++) {
                select.setObject(j + 1, fieldsConditions[i]);
            }

            resultSet = select.executeQuery();
            connection.commit();

            return resultSet;
        } catch (SQLException exception) {
            logger.getExceptionTextFileLogger().error(exception);
            if (connection != null) {
                try {
                    connection.rollback();
                    logger.getExceptionTextFileLogger().info("Transaction is being rolled back");
                    if (select != null) {
                        select.close();
                    }
                    resultSet.close();
                } catch (SQLException e) {
                    logger.getExceptionTextFileLogger().error(e);
                }
            }
            return null;
        } finally {
            connection.setAutoCommit(true);
        }
    }

    public ResultSet select(String request) {
        Connection connection = super.getPool().createConnection();
        ResultSet resultSet = null;
        try {
            Statement statement;
            statement = connection.createStatement();
            resultSet = statement.executeQuery(request);
            return resultSet;
        } catch (SQLException exception) {
            logger.getExceptionTextFileLogger().error("Can`t select from the database." + exception);

            if (connection != null) {
                try {
                    logger.getExceptionTextFileLogger().info("Transaction is being rolled back");
                    connection.rollback();
                } catch (SQLException e) {
                    logger.getExceptionTextFileLogger().error(e);
                }
            }
            return null;
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException e) {
                logger.getExceptionTextFileLogger().error(e);
            }
        }
    }

}